<template>
  <div class="login_container">
    <div class="login_box">
        <!-- 头像区 -->
        <div class="avator_box">
          <img src="../assets/img/logo.png" alt="">
        </div>
        <!-- 登录表单区 -->
        <el-form ref="loginFormRef" :model="user" class="loginForm" :rules="rules">
          <!-- 登录邮箱 -->
          <el-form-item prop="uMail">
            <el-input placeholder="请输入东软邮箱" v-model="user.uMail" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="uPwd">
            <el-input placeholder="请输入邮箱密码" v-model="user.uPwd" prefix-icon="iconfont icon-3702mima" type="password" show-password></el-input>
          </el-form-item>
          <!-- 按钮区域 -->
          <el-form-item class="btns">
            <el-button type="primary" @click="onLogin">登录</el-button>
            <el-button type="info" @click="resetLogin">取消</el-button>
            <el-button type="success" @click="toRegister">注册</el-button>
          </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        user: {
          uMail: this.$route.query.umail,
          uPwd: this.$route.query.upwd
        },
        //表单验证规则对象
        rules: {
          //验证邮箱是否合法
          uMail: [
            { required: true, message: '请输入东软邮箱账号', trigger: "blur" },
            { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
          ],
          //验证密码是否合法
          uPwd: [
            { required: true, message: '请输入东软邮箱密码', trigger: "blur"},
            { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: ['blur', 'change'] }
          ]
        }
      }
    },
    methods: {
      resetLogin() {
        this.$refs.loginFormRef.resetFields();
      },
      onLogin() {
        this.$refs.loginFormRef.validate((valid) => {
          if(!valid) return;
          this.doLogin();
        })
      },
      async doLogin(){
        let url = 'api/userLogin/login';
        let params = this.$qs.stringify({
          邮箱: this.user.uMail,
          密码: this.user.uPwd
        })
        
        const {data: res} = await this.$axios.post( url, params );
        if(res.code !== 200){
          this.resetLogin();
          return this.$message.error('邮箱或密码错误，登录失败');
        } else {
            this.$message.success('登录成功')
            window.sessionStorage.setItem(
              'token',
              res.data.token
            )
            //登录用户id存入sessionStorage
            window.sessionStorage.setItem('loginUserId', res.data.user.userId);
            window.sessionStorage.setItem('loginUserUmail', res.data.user.umail);
            window.sessionStorage.setItem('uverify', res.data.user.uverify);
            window.sessionStorage.setItem('oldPwd', res.data.user.upwd);
            console.log(res.data.user.uverify)
            this.$router.push('/home');
        }
      },
      toRegister(){
        this.$router.push('/register')
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container{
    background-color: #eaeef1;
    height: 100%;
    background-image: url('../assets/img/log-login.png');
    background-size: cover;
  }
  .login_box{
    width: 450px;
    height: 280px;
    background-color: #fff;
    border-radius: 3px;
    padding: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    opacity: 0.9;

    .avator_box{
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
  .loginForm{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 30px;
    box-sizing: border-box;
  }
</style>